package edu.gatech.spacetraderteam3.view;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 * A class that represents a label on the GUI
 * @author Alexander Ikonomidis
 * @version 1.0
 */
public class SGGLabel extends SGGComponent {

    /**
     * the width of a line of the label
     */
    private int lineWidth = Integer.MAX_VALUE; // Not yet used

    /**
     * The text displayed in the label
     */
    private String text;

    /**
     * The background image of the label
     */
    private BufferedImage img;

    /**
     * Constructor for a label that displays text
     * @param text Text of the label
     */
    public SGGLabel(String text) {
        super(100, 10);
        this.text = text;
    }

    /**
     * Constructor for a label that displays an image. 
     * @param img Image to display
     */
    public SGGLabel(BufferedImage img) {
        super(img.getWidth(), img.getHeight());
        this.img = img;
    }

    /**
     * Paints the label. Wraps around if length becomes too long. 
     * @param g Graphics component to paint on
     */
//    public void doPaint(Graphics2D g) {
//        if (img == null) {
//            g.setColor(color);
//            String text2 = text;
//            StringBuffer line = new StringBuffer("");
//            lineWidth = Math.max(lineWidth, 1);// To prevent weird problems. Could be a thing to note later on.
//            int lineNum = 0;
//            final int textHeight = g.getFontMetrics().getHeight();
//            while (text2.length() > 0) {
//                while (line.length() < lineWidth && text2.length() > 0) {
//                    line.append(text2.charAt(0));
//                    text2 = text2.substring(1);
//                }
//                g.drawString(line.toString().trim(), getX(), getY() + getHeight()
//                        + textHeight * lineNum + 2);
//                line = new StringBuffer("");
//                lineNum++;
//            }
//        } else {
//            g.drawImage(img, getX(), getY(), null);
//        }
//    }
    
    public void doPaint(Graphics2D g) {
        if (img == null) {
            g.setColor(color);
            String[] text2 = text.split("\\s");
            StringBuffer line = new StringBuffer("");
            lineWidth = Math.max(lineWidth, 1);// To prevent weird problems. Could be a thing to note later on.
            int lineNum = 0;
            int tokenNum = 0;
            String curToken;
            final int textHeight = g.getFontMetrics().getHeight();
            while (text2.length > tokenNum) {
                while (text2.length > tokenNum && (curToken = text2[tokenNum]) != null && line.length() + curToken.length() < lineWidth) {
                    line.append(curToken + " ");
                    tokenNum++;
                }
                g.drawString(line.toString().trim(), getX(), getY() + getHeight()
                        + textHeight * lineNum + 2);
                line = new StringBuffer("");
                lineNum++;
            }
        } else {
            g.drawImage(img, getX(), getY(), null);
        }
    }

    /**
     * Setter for the label's text
     * @param text Text to display
     */
    public void setText(String text) {
        this.text = text;
        img = null;
    }

    /**
     * Setter for the label's image
     * @param img Image to draw
     */
    public void setImage(BufferedImage img) {
        this.img = img;
    }

    /**
     * Setter for the width of a line
     * @param lineWidth Width of a line
     */
    public void setLineWidth(int lineWidth) {
        this.lineWidth = lineWidth;
    }

    /**
     * @return the text on the label
     */
    public String toString() {

        return text;
    }
}
